package org.jeecg.modules.bv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.bv.entity.BvStorages;
import org.jeecg.modules.bv.entity.BvVouchers;
import org.jeecg.modules.bv.mapper.BvStoragesMapper;
import org.jeecg.modules.bv.service.IBvStoragesService;
import org.jeecg.modules.bv.service.IBvVouchersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 票据入库
 * @Author: jeecg-boot
 * @Date:   2022-03-09
 * @Version: V1.0
 */
@Service
public class BvStoragesServiceImpl extends ServiceImpl<BvStoragesMapper, BvStorages> implements IBvStoragesService {

    @Autowired
    private BvStoragesMapper bvStoragesMapper;
    @Autowired
    private IBvVouchersService bvVouchersService;

    @Override
    @Transactional
    public void mainSave(BvStorages bvStorages) {
        BigDecimal subtract = bvStorages.getEndNo().subtract(bvStorages.getBeginNo());
        subtract=subtract.add(new BigDecimal(1.0));//加1 开始结束都是包含的
        bvStorages.setStoragedAmount(subtract);
        bvStoragesMapper.insert(bvStorages);

        int amount = subtract.intValue();
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        BigDecimal beginNo = bvStorages.getBeginNo();

        for(int i=0;i<amount;i++){
            BvVouchers vouchers = new BvVouchers();
            vouchers.setBillTypeId(bvStorages.getBillTypeId());
            vouchers.setVersionId(bvStorages.getVersionId());
            vouchers.setVoucherNo(beginNo);
            //入库ID
            vouchers.setStorageId(bvStorages.getId());

            vouchers.setBillStatusId("01");//入库
            vouchers.setStatus("1");//正常
            vouchers.setGetOperatId(sysUser.getUsername());

            bvVouchersService.save(vouchers);

            beginNo=beginNo.add(new BigDecimal(1.0));

        }
    }

    @Override
    @Transactional
    public Result<?> mainUpdate(BvStorages bvStorages) {

        int count = bvVouchersService.count(new LambdaQueryWrapper<BvVouchers>()
                .eq(BvVouchers::getStorageId, bvStorages.getId())
                .gt(BvVouchers::getBillStatusId, "01") //发票大于 01入库状态，表示已使用了
                .eq(BvVouchers::getStatus, "1")
                .eq(BvVouchers::getDelFlag, 0)       );
        if(count>0){
            return Result.error("已产生业务不允许编辑");
        }

        BigDecimal subtract = bvStorages.getEndNo().subtract(bvStorages.getBeginNo());
        subtract=subtract.add(new BigDecimal(1.0));//加1 开始结束都是包含的
        bvStorages.setStoragedAmount(subtract);
        bvStoragesMapper.updateById(bvStorages);

        //先删除
        List<BvVouchers> vouchersList = bvVouchersService.list(new LambdaQueryWrapper<BvVouchers>()
                .eq(BvVouchers::getStorageId, bvStorages.getId())
                .eq(BvVouchers::getStatus, "1")
                .eq(BvVouchers::getDelFlag, 0));
        if(null != vouchersList && vouchersList.size()>0){
            List<String> ids = vouchersList.stream().map(BvVouchers::getId).collect(Collectors.toList());
            bvVouchersService.delBatchMain(ids);
        }

        //再新增
        int amount = subtract.intValue();
        BigDecimal beginNo = bvStorages.getBeginNo();
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        for(int i=0;i<amount;i++){
            BvVouchers vouchers = new BvVouchers();
            vouchers.setBillTypeId(bvStorages.getBillTypeId());
            vouchers.setVersionId(bvStorages.getVersionId());
            vouchers.setVoucherNo(beginNo);
            //入库ID
            vouchers.setStorageId(bvStorages.getId());

            vouchers.setBillStatusId("01");//入库
            vouchers.setStatus("1");//正常
            vouchers.setGetOperatId(sysUser.getUsername());

            bvVouchersService.save(vouchers);

            beginNo=beginNo.add(new BigDecimal(1.0));

        }
        return Result.OK("编辑成功!");
    }

    @Override
    public Result<?> mainRemove(String id) {
        int count = bvVouchersService.count(new LambdaQueryWrapper<BvVouchers>()
                .eq(BvVouchers::getStorageId, id)
                .gt(BvVouchers::getBillStatusId, "01") //发票大于 01入库状态，表示已使用了
                .eq(BvVouchers::getStatus, "1")
                .eq(BvVouchers::getDelFlag, 0)       );
        if(count>0){
            return Result.error("已产生业务不允许删除");
        }

        //先删除
        List<BvVouchers> vouchersList = bvVouchersService.list(new LambdaQueryWrapper<BvVouchers>()
                .eq(BvVouchers::getStorageId, id)
                .eq(BvVouchers::getStatus, "1")
                .eq(BvVouchers::getDelFlag, 0));
        if(null != vouchersList && vouchersList.size()>0){
            List<String> ids = vouchersList.stream().map(BvVouchers::getId).collect(Collectors.toList());
            bvVouchersService.delBatchMain(ids);
        }

        bvStoragesMapper.deleteById(id);

        return Result.OK("删除成功!");
    }
}
